//
//  FSMeetingOpinionController.m
//  Trade
//


//已收文件的填写意见环节

#import "GDataDefines.h"

#import "HttpTools.h"
#import "Toast+UIView.h"
#import "FSMeetingOpinionController.h"
#import "FSInputOpinionView.h"
#import "FSInputRootView.h"

@interface FSMeetingOpinionController ()

//人员详情
@property (weak, nonatomic) IBOutlet FSInputRootView *inputRootView;


@property (weak, nonatomic) IBOutlet UITextView *textView;


- (IBAction)addAction:(id)sender;
@property(nonatomic,copy)NSString * body;

@end

@implementation FSMeetingOpinionController

- (void)viewDidLoad {
   //注册键盘出现的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    //注册键盘消失的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification object:nil];


    [super viewDidLoad];
    
    [self setRightItem];
}
-(void)keyboardWillShow:(NSNotification *)notification
{
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0);





//    NSDictionary *userInfo = note.userInfo;
//    CGFloat duration = [userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
//
//    CGRect keyFrame = [userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
//    CGFloat moveY = keyFrame.origin.y - self.view.frame.size.height;//这个64是我减去的navigationbar加上状态栏20的高度,可以看自己的实际情况决定是否减去;
//
//    [UIView animateWithDuration:duration animations:^{
//        self.view.transform = CGAffineTransformMakeTranslation(0, moveY);
//    }];
}

//键盘隐藏后将视图恢复到原始状态
-(void)keyboardWillHide:(NSNotification *)note
{
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeView" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [UIView commitAnimations];
}

- (void)viewWillAppear:(BOOL)animated{

   [self requestData];
}
-(void)requestData{
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unid":self.identifier},@{@"userId":userId}];
    
    NSString * url = nil;
    NSString * method = nil;
  url = @"exchexchange";
  method = @"getMeetSuggestion";
    [HttpTools requestWithURL:url arrays:array method:method success:^(id JSON) {

    

        NSArray * dataArrary = JSON[@"data"];
        if (dataArrary.count) {
        for(int i = 1;i<dataArrary.count;i++){
              [self.inputRootView addInputOpinionView];

            }

            [self.inputRootView updata:dataArrary];
                [self.tableView reloadData];

                    }
//                    else{
//
//                                  [self.inputRootView addInputOpinionView];
//                [self.tableView reloadData];
//
//
//                    }
        
    } failure:^(NSError *error) {
 
    }];
    
}


-(void)setRightItem{
    
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(submitAction)];
    
    self.navigationItem.rightBarButtonItem = rightItem;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 84;
    }else if (indexPath.row == 1) {
        return [self.inputRootView inputRootViewHeight];
    }else if (indexPath.row == 2){
        return 135;
    }
    return 44;
}

//增加按钮
- (IBAction)addAction:(id)sender {
    
    [self.inputRootView addInputOpinionView];
    [self.tableView reloadData];
//    [self.tableView layoutIfNeeded];
    
} 

-(void)submitAction{
    NSArray * inputInfoArray = [self.inputRootView getInfo];


if (inputInfoArray.count>0) {
    

    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

    [self.view endEditing:YES];
    
    NSMutableArray *name = [NSMutableArray array];
     NSMutableArray *job = [NSMutableArray array];
      NSMutableArray *phone = [NSMutableArray array];
            NSMutableArray *beizu = [NSMutableArray array];

      for(int i = 0;i<inputInfoArray.count;i++){
    
NSDictionary *dic = inputInfoArray[i];
if (dic.count>0) {
    
_type = @"yes";
[name addObject:dic[@"name"]];
[job addObject:dic[@"jobTitle"]];
[phone addObject:dic[@"connectMetod"]];
[beizu addObject:dic[@"demo"]];

}else{

        [self.view.window makeToast:@"请填写信息"];

}

            }
            if ([_type isEqualToString:@"yes"]) {
    

NSString *namestring = [name componentsJoinedByString:@","];
    NSString *jobstring = [job componentsJoinedByString:@","];
    NSString *phonestring = [phone componentsJoinedByString:@","];
  NSString *beizustring = [beizu componentsJoinedByString:@","];

    
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:inputInfoArray forKey:@"info"];
    if (self.textView.text.length !=0) {
        [dict setObject:self.textView.text forKey:@"reason"];
    }else{
        [dict setObject:@"" forKey:@"reason"];
    }
    
    NSData * data  = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];
    NSString * infoString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
      NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
   

  
 self.body = [NSString stringWithFormat:@"&lt;?xml version=\'1.0\' encoding=\'utf-8\'?&gt;\n"
                     "&lt;交换 xmlns=\'exch.jhsx.gov.cn/data\' xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\'&gt;\n"
                     "&lt;数据&gt;\n"
                     "&lt;数据要素&gt;\n"
                     "&lt;姓名&gt;\n"
                            "%@"
                     "&lt;/姓名&gt;\n"
                     "&lt;职务&gt;\n"
                           "%@"
                     "&lt;/职务&gt;\n"
                     "&lt;联系方式&gt;\n"
                         "%@"
                     "&lt;/联系方式&gt;\n"
                      "&lt;备注&gt;\n"
                         "%@"
                     "&lt;/备注&gt;\n"
                      "&lt;不参加会议原因&gt;\n"
                           "%@"
                     "&lt;/不参加会议原因&gt;\n"
                     "&lt;/数据要素&gt;\n"
                     "&lt;/数据&gt;\n"
                     "&lt;/交换&gt;",
                   namestring,
                     jobstring,
                    phonestring,
                    beizustring,
                    self.textView.text
                     ];
    
//     NSArray * array =@[@{@"userId":userId},@{@"unid":self.identifier},@{@"formxml":_body}];
    
          NSArray * array =@[@{@"unid":self.identifier},@{@"userId":userId},@{@"formxml":_body}];

    
      [HttpTools postWithURL:KexchexchangeURL arrays:array method:KSetSubmitMeeting success:^(id JSON) {
              [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
 GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: [JSON dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
        GDataXMLElement *rootElement = [doc rootElement];
        NSArray *arrData= [rootElement elementsForName:@"info"];
        NSString * message= nil;
        for (GDataXMLElement * subXMLElement in arrData) {
            GDataXMLElement * element = [[subXMLElement elementsForName:@"msg"] objectAtIndex:0];
            message  = [element stringValue];
        }
        NSLog(@"%@",message);
if ([message isEqualToString:@"33"]) {
            [self.view.window makeToast:@"提交意见成功"];
                    [self.navigationController popViewControllerAnimated:YES];
}else{

[self.view.window makeToast:@"提交意见失败"];
}
    } failure:^(NSError *error) {
                  [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];

        [self.view.window makeToast:@"提交意见失败"];
    }];
    }
}else{

        [self.view.window makeToast:@"请填写信息"];

}
}


@end

















